package com.verlet.servicehi;

import com.verlet.servicehi.converter.TextMessageConverter;
import com.verlet.servicehi.delegate.MessageDelegate;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
import org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * rabbitMQ配置类
 *
 * @author verlet
 * @date 2020/1/7
 */
@Configuration
public class RabbitMqConfiguration {

//    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setHost("localhost");
        cachingConnectionFactory.setPort(5672);
        cachingConnectionFactory.setUsername("admin");
        cachingConnectionFactory.setPassword("admin");
        cachingConnectionFactory.setVirtualHost("rabbitmq");
        return cachingConnectionFactory;
    }

//    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        // 必须设置为true，否则Spring容器不会加载RabbitAdmin类
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange("test001.topic.exchange", false, false);
    }

    @Bean
    public Queue queue() {
        return new Queue("test001.topic.queue", false);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(exchange()).with("test001.#");
    }

//    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

//    @Bean
    public SimpleMessageListenerContainer messageContainer(ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer messageContainer = new SimpleMessageListenerContainer(connectionFactory);
        messageContainer.setQueues(queue());
        messageContainer.setConcurrentConsumers(1);
        messageContainer.setMaxConcurrentConsumers(5);
        messageContainer.setDefaultRequeueRejected(false);
        messageContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
        messageContainer.setConsumerTagStrategy(queue -> queue + "_" + UUID.randomUUID().toString());
        /**
         messageContainer.setMessageListener(new ChannelAwareMessageListener() {
        @Override public void onMessage(Message message, AMQP.Channel channel) throws Exception {
        System.out.println(new String(message.getBody()));
        }
        });
         */
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(new MessageDelegate());
        messageListenerAdapter.setDefaultListenerMethod("consumeMessage");

        /**
        ContentTypeDelegatingMessageConverter contentTypeDelegatingMessageConverter = new ContentTypeDelegatingMessageConverter();

        TextMessageConverter textMessageConverter = new TextMessageConverter();
        contentTypeDelegatingMessageConverter.addDelegate(MessageProperties.CONTENT_TYPE_TEXT_PLAIN, textMessageConverter);
        contentTypeDelegatingMessageConverter.addDelegate(MessageProperties.CONTENT_TYPE_XML, textMessageConverter);

         Json转Java对象
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
        DefaultJackson2JavaTypeMapper defaultJackson2JavaTypeMapper = new DefaultJackson2JavaTypeMapper();
        Map<String, Class<?>> idClassMapping = new HashMap();
        idClassMapping.put("messageDelegate", MessageDelegate.class);
        defaultJackson2JavaTypeMapper.setIdClassMapping(idClassMapping);
        jackson2JsonMessageConverter.setJavaTypeMapper(defaultJackson2JavaTypeMapper);
        contentTypeDelegatingMessageConverter.addDelegate(MessageProperties.CONTENT_TYPE_JSON,jackson2JsonMessageConverter);
        */
        messageListenerAdapter.setMessageConverter(new TextMessageConverter());
        messageContainer.setMessageListener(messageListenerAdapter);
        return messageContainer;
    }
}
